<?php
namespace Admin\Service;

class AdminService
{
    /**
     * 后台管理员登录
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function login($username = '', $password = '', $online = 1)
    {
        $admin = M('admin_user')
            ->where([
                'username' => $username,
            ])
            ->find();

        if (!$admin) {
            return false;
        }

        if ($admin['password'] != admin_password($password, $admin['salt'])) {
            return false;
        }

        session('admin_user', $admin);
        if ($online) {
            $crypt = new \Think\Crypt();
            $str   = $crypt->encrypt($username . '/' . $password, C('ENCRYPT_KEY'));
            cookie('logining_admin', $str);
        }

        return true;
    }
}
